// pages/lygPage1/lygPage1.js
var app = getApp();
const util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    httpApiImg: app.globalData.httpApiImg,
    userInfo: '',
    title2: '',
    multiArray: [['1室', '2室', '3室', '4室', '5室', '5室以上'], ['0厅', '1厅', '2厅', '3厅', '3厅以上'], ['0卫', '1卫', '2卫', '3卫', '3卫以上']],
    multiSchool: [['海州区', '连云区', '赣榆区'], ["解放路教育集团高新区校区", "解放路教育集团郁洲分校", "解放路教育集团本部", "新浦实验小学", "滨河小学", "师专二附小教育集团孔望山小学", "师专二附小教育集团本部", "连云港实验学校小学部", "师专一附小教育集团盐河校区", "师专一附小教育集团龙河校区", "凤凰学校（小学部）", "苍梧小学", "向阳小学", "建宁小学", "海宁小学", "建国路小学", "大庆路小学", "幸福路小学", "新海小学", "大村中心小学", "南城中心小学", "海州实验小学", "海州第二实验小学", "许庄小学", "宁海中心小学", "新浦中学", "海庆中学", "海宁中学", "新海实验中学教育集团延安校区", "新海实验中学教育集团苍梧校区", "外国语学校", "凤凰学校（中学部）", "连云港实验学校中学部", "花果山中学", "海州实验中学", "宁海中学"]],
    formlist: [
      { isrequire: true, value: '', label: '租赁方式', valueType: 'text', key: 'leaseWay', tips: '请填写租赁方式', type: 'select', data: ['合租', '整租'], isIndex: true },
      { isrequire: true, value: '', label: '面积', valueType: 'digit', key: 'area', tips: '请填写面积', type: 'input', right: '平米' },
      { isrequire: true, value: '', label: '租金', valueType: 'digit', key: 'rent', tips: '请填写租金', type: 'input', right: '元' },
      { isrequire: true, value: '', label: '付款方式', valueType: 'text', key: 'payWay', tips: '请选择付款方式', type: 'select', data: ['季付', '月付'], isIndex: true },
      { isrequire: true, value: '', label: '所属楼层', valueType: 'number', key: 'floor', tips: '请选择所属楼层', type: 'select', data: [] },
      { isrequire: true, value: '', label: '总楼层', valueType: 'number', key: 'totalFloor', tips: '请选择总楼层', type: 'select', data: [] },
      { isrequire: true, value: '', label: '装修', valueType: 'text', key: 'decorate', tips: '请选择装修', type: 'select', data: ['毛坯', '简装', '精装', '豪华装'], isIndex: true },
      { isrequire: true, value: '', label: '朝向', valueType: 'text', key: 'toward', tips: '请选择朝向', type: 'select', data: ['南北通透', '南', '东', '西', '北'] },
      { isrequire: true, value: '', label: '区域', valueType: 'text', key: 'region', tips: '请选择区域', type: 'select', data: ["海州区", "连云区", "赣榆区"] },
      { isrequire: false, value: '', label: '地铁', valueType: 'text', key: 'subway', tips: '请选择地铁', type: 'select', data: ['无'] },
      { isrequire: false, value: '', label: '学区', valueType: 'text', key: 'schoolDistrict', tips: '请选择学区', type: 'select', data: [] },
      { isrequire: true, value: '', label: '年份', valueType: 'number', key: 'year', tips: '请填写年份', type: 'select', mode: 'date', fields: 'year' }
    ],
    formData: {
      city: '连云港',
      releaseUserId: '', //	发布人id	
      houseSourceTypeId: '', //	房源类型（调接口获得id）	
      picture: '', //	图片路径，逗号分隔开	
      communityName: '', //	小区名称	
      businessType: '', //	商业类型	(0住宅底商，1，连家店)
      houseDoor: '', //	楼牌门洞	
      room: '', //	室	
      hall: '', //	厅	
      toilet: '', //	卫	
      leaseWay: '', //	租赁方式（0合租，1整租）
      area: '', //	面积	
      rent: '', //	租金
      rentType: '', //	租金方式（单位：0 元/ 月，1 元/ 天 * 平米） 没有选择默认 赋值为 0
      canBeRegistered: '', //	可注册	(0 不可注册, 1 可注册)	
      payWay: '', //	付款方式 （0 季付，1 月付）	
      parkingProperties: '', //	车位属性（0 车位，1 车库）	
      floor: '', //	所属楼层	
      totalFloor: '', //	总楼层	
      decorate: '', //	装修（0毛坯、1简装、2精装、3豪华装）	
      divisionStatus: '', //	是否可分割 （0不可分割，1可分割）	
      toward: '', //	朝向	
      year: '', //	年份	
      region: '', //	地区	
      subway: '', //	地铁	
      schoolDistrict: '', //	学区	
      maintenanceFee: '', //	物业费
      contact: '', //	联系人	
      phone: '', //	手机号	
      title: '', //	标题	
      housingDescription: '', //	房源描述
      housingCharacteristics: '', //	房源特色
    },
    show: {
      houseType: '',
      houseDoor1: '',
      houseDoor2: '',
      houseDoor3: '',
      room: '',
      hall: '',
      toilet: '',
      leaseWay: '',
      payWay: '',
      decorate: '',
      businessType: '',
      rentType: ''
    },
    imgList: [],
    keys: '',
    inputShow: '',
    value: '',
    currentInput: '',
    active: '',
    canSubmit: false,
    houseTypeList: [{ id: 1, typeName: '新房' }, { id: 2, typeName: '二手房' }],
    bussTypeList: ['住宅底商', '连家店']
  },
  bindInputFunc: function (e) {
    let key = e.currentTarget.dataset.key;
    this.data.formData[key] = e.detail.value;
    this.setData({ formData: this.data.formData });
    this.changeValue();
  },
  bindBussChange: function (e) {
    var selectedItem = this.data.bussTypeList[e.detail.value];
    this.setData({
      ['show.businessType']: selectedItem,
      ['formData.businessType']: e.detail.value
    })
    this.changeValue();
  },
  bindTypeChange: function (e) {
    var selectedItem = this.data.houseTypeList[e.detail.value];
    this.setData({
      ['show.houseType']: selectedItem.typeName,
      ['formData.houseType']: selectedItem.id
    })
    this.changeValue();
  },
  schoolChange: function (e) {
    this.setData({
      ['formData.schoolDistrict']: this.data.multiSchool[1][e.detail.value[1]]
    })
  },
  bindcolumnchange: function (e) {
    var arr = [];
    if (e.detail.column == 0) {
      if (e.detail.value == 0) {
        arr = ["解放路教育集团高新区校区", "解放路教育集团郁洲分校", "解放路教育集团本部", "新浦实验小学", "滨河小学", "师专二附小教育集团孔望山小学", "师专二附小教育集团本部", "连云港实验学校小学部", "师专一附小教育集团盐河校区", "师专一附小教育集团龙河校区", "凤凰学校（小学部）", "苍梧小学", "向阳小学", "建宁小学", "海宁小学", "建国路小学", "大庆路小学", "幸福路小学", "新海小学", "大村中心小学", "南城中心小学", "海州实验小学", "海州第二实验小学", "许庄小学", "宁海中心小学", "新浦中学", "海庆中学", "海宁中学", "新海实验中学教育集团延安校区", "新海实验中学教育集团苍梧校区", "外国语学校", "凤凰学校（中学部）", "连云港实验学校中学部", "花果山中学", "海州实验中学", "宁海中学"]
      }
      else if (e.detail.value == 1) {
        arr = ["墟沟小学", "东港小学", "院前小学", "南巷小学", "海州湾小学", "陶庵小学", "庙岭小学", "临海路小学", "连岛小学", "云山小学", "白果树小学", "宿城小学", "板桥小学", "东港中学", "西苑中学", "新港中学", "连云中学", "板桥中学"]
      }
      else if (e.detail.value == 2) {
        arr = ["实验小学", "青口中心小学（新青小）", "青口小学（老青小）", "宁海路小学", "小荒小学", "大朱旭小学", "黄海路小学", "新城实验小学", "实验中学", "外国语中学"]
      }
      this.data.multiSchool.splice(1, 1, arr);
      this.setData({
        multiSchool: this.data.multiSchool
      })
    }
  },
  otherInput: function (e) {
    var key = e.currentTarget.dataset.key;
    this.setData({
      [key]: e.detail.value
    })
    this.changeValue();
  },
  // 户型
  bindMultiPickerChange: function (e) {
    var arr = e.detail.value
    this.setData({
      ['formData.room']: this.data.multiArray[0][arr[0]].split("室")[0],
      ['show.room']: this.data.multiArray[0][arr[0]],
      ['formData.hall']: this.data.multiArray[1][arr[1]].split("厅")[0],
      ['show.hall']: this.data.multiArray[1][arr[1]],
      ['formData.toilet']: this.data.multiArray[2][arr[2]].split("卫")[0],
      ['show.toilet']: this.data.multiArray[2][arr[2]]
    })
    if (this.data.multiArray[0][arr[0]].indexOf("以上") > 0) {
      this.setData({
        ['formData.room']: this.data.formData.room + 1
      })
    }
    if (this.data.multiArray[1][arr[1]].indexOf("以上") > 0) {
      this.setData({
        ['formData.hall']: this.data.formData.hall + 1
      })
    }
    if (this.data.multiArray[2][arr[2]].indexOf("以上") > 0) {
      this.setData({
        ['formData.toilet']: this.data.formData.toilet + 1
      })
    }
    this.changeValue();
  },
  // 添加照片
  addPhoto: function () {
    wx: wx.navigateTo({
      url: '/pages/addPhoto/addPhoto'
    })
  },
  changeValue: function () {
    var flag = true;
    var item = this.data.formData;
    if (this.data.title2 == '住宅') {
      if ((item.communityName === '') || (item.leaseWay === '') || (item.area === '') || (item.rent === '') || (item.payWay === '') || (item.floor === '') || (item.totalFloor === '') || (item.decorate === '') || (item.toward === '') || (item.region === '') || (item.year === '')) {
        flag = false;
      }
    }
    if (this.data.title2 == '别墅') {
      if ((item.communityName === '') || (item.room === '') || (item.leaseWay === '') || (item.area === '') || (item.rent === '') || (item.payWay === '') || (item.floor === '') || (item.totalFloor === '') || (item.decorate === '') || (item.toward === '') || (item.region === '') || (item.year === '')) {
        flag = false;
      }
    }
    if (this.data.title2 == '写字楼') {
      if ((item.communityName === '') || (item.leaseWay === '') || (item.area === '') || (item.rent === '') || (item.payWay === '') || (item.floor === '') || (item.totalFloor === '') || (item.divisionStatus === '') || (item.maintenanceFee === '') || (item.decorate === '') || (item.region === '') || (item.subway === '') || (item.year === '')) {
        flag = false;
      }
    }
    if (this.data.title2 == '商铺') {
      if ((item.communityName === '') || (item.businessType === '') || (item.rentType === '') || (item.leaseWay === '') || (item.area === '') || (item.rent === '') || (item.payWay === '') || (item.floor === '') || (item.totalFloor === '') || (item.decorate === '') || (item.region === '') || (item.subway === '') || (item.year === '')) {
        flag = false;
      }
    }
    if (this.data.title2 == '厂房仓库') {
      if ((item.communityName === '') || (item.leaseWay === '') || (item.area === '') || (item.rent === '') || (item.payWay === '') || (item.floor === '') || (item.totalFloor === '') || (item.region === '') || (item.subway === '') || (item.year === '')) {
        flag = false;
      }
    }
    if (this.data.title2 == '车库车位') {
      if ((item.communityName === '') || (item.area === '') || (item.rent === '') || (item.payWay === '') || (item.region === '') || (item.subway === '') || (item.year === '')) {
        flag = false;
      }
    }
    this.setData({
      canSubmit: flag
    })
  },
  bindPickerChange: function (e) {
    var item = e.target.dataset.item;
    if (e.target.dataset.type != undefined) {
      this.data.formData[item.key] = e.detail.value;
    }
    else {
      if (item.isIndex) {
        this.data.formData[item.key] = e.detail.value;
        this.data.show[item.key] = this.data.formlist[e.target.dataset.index].data[e.detail.value];
        this.setData({
          show: this.data.show
        })
      }
      else {
        this.data.formData[item.key] = this.data.formlist[e.target.dataset.index].data[e.detail.value];
      }
      if (item.label == '所属楼层') {
        var arr = [];
        for (var i = this.data.formData[item.key]; i < 101; i++) {
          arr.push(i)
        }
        if (this.data.title2 == '住宅' || this.data.title2 == '别墅' || this.data.title2 == '厂房仓库') {
          this.bindobj('formlist', arr, 'data', 5);
        }
        else if (this.data.title2 == '写字楼' || this.data.title2 == '商铺') {
          this.bindobj('formlist', arr, 'data', 6);
        }
      }
    }

    this.setData({
      formData: this.data.formData
    });
    this.changeValue();
  },

  // 下一步
  nextStep: function () {
    if (this.data.canSubmit) {
      if (this.data.show.houseDoor1) {
        this.data.formData.houseDoor = this.data.show.houseDoor1 + '栋/号楼' + this.data.show.houseDoor2 + '单元' + this.data.show.houseDoor3 + '室';
      }
      this.data.formData.unitPrice = (this.data.formData.totalMoney / this.data.formData.area * 10000).toFixed(2);
      this.data.formData.picture = this.data.imgList.join(",");
      wx.setStorageSync('house', this.data.formData);
      wx.navigateTo({
        url: '/pages/houseNext/houseNext?title2=' + this.data.title2 + '&isRent=true'
      })
    }

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var key1 = 'formData.releaseUserId';

    this.setData({
      ['formData.houseSourceTypeId']: options.houseSourceTypeId,
      title2: options.title,
      [key1]: JSON.parse(wx.getStorageSync('userInfo')).id,
      userInfo: JSON.parse(wx.getStorageSync('userInfo'))
    })
    wx.setNavigationBarTitle({
      title: options.title
    })
    if (this.data.title2 == '写字楼') {
      var obj = { isrequire: false, value: '', label: '可注册', valueType: 'text', key: 'canBeRegistered', tips: '请选择可注册', type: 'select', data: ['可注册公司', '不可注册公司'] };
      this.data.formlist.splice(2, 0, obj);
      this.data.formlist.splice(8, 1);
      this.data.formlist.splice(10, 1);
      // this.data.formlist.splice(7, 1);
      var obj1 = { isrequire: true, value: '', label: '是否可分割', valueType: 'text', key: 'divisionStatus', tips: '请填写是否可分割', type: 'select', data: ['不可分割', '可分割'] };
      this.data.formlist.splice(7, 0, obj1);
      var obj2 = { isrequire: true, value: '', label: '物业费', valueType: 'text', key: 'maintenanceFee', tips: '请填写物业费', type: 'input', right: '元/月*平方米', largeright: true };
      this.data.formlist.splice(8, 0, obj2);
    }
    else if (this.data.title2 == '商铺') {
      var obj = { isrequire: true, value: '', label: '租金方式', valueType: 'text', key: 'rentType', tips: '请选择租金方式', type: 'select', data: ['元/月', '元/天*平米'], isIndex: true };
      this.data.formlist.splice(3, 0, obj);
      this.data.formlist.splice(8, 1);
      this.data.formlist.splice(10, 1);
      var obj1 = { isrequire: true, value: '', label: '物业费', valueType: 'text', key: 'maintenanceFee', tips: '请填写物业费', type: 'input', right: '元/月*平方米', largeright: true };
      this.data.formlist.splice(8, 0, obj1);
    }
    else if (this.data.title2 == '厂房仓库') {
      this.data.formlist.splice(6, 2);
      this.data.formlist.splice(8, 1);
    }
    else if (this.data.title2 == '车库车位') {
      this.data.formlist.splice(0, 1);
      this.data.formlist.splice(3, 2);
      var obj = { isrequire: true, value: '', label: '车位属性', valueType: 'text', key: 'parkingProperties', tips: '请选择车位属性', type: 'select', data: ['车库', '车位'] };
      this.data.formlist.splice(3, 0, obj);
      this.data.formlist.splice(4, 2);
      this.data.formlist.splice(6, 1);
    }
    this.setData({
      formlist: this.data.formlist
    })
    //wx.clearStorage();
  },
  bindobj: function (oldval, newval, key, index) {
    var arr = [];
    arr = this.data[oldval];
    arr[index][key] = newval;
    this.setData({
      formlist: arr
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var arr = [];
    for (var i = 1; i < 101; i++) {
      arr.push(i)
    }
    if (this.data.title2 == '住宅' || this.data.title2 == '别墅' || this.data.title2 == '厂房仓库') {
      this.bindobj('formlist', arr, 'data', 4);
      this.bindobj('formlist', arr, 'data', 5);
    }
    else if (this.data.title2 == '写字楼' || this.data.title2 == '商铺') {
      this.bindobj('formlist', arr, 'data', 5);
      this.bindobj('formlist', arr, 'data', 6);
    }

    //wx.clearStorage();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var img = wx.getStorageSync('img');
    if (img) {
      this.setData({
        imgList: img.split(",")
      })
    }
    else {
      this.setData({
        imgList: ''
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})